//
//  WatchViewModel.swift
//  WatchShouChong Watch App
//
//  Created by yw on 2024/8/19.
//

import Foundation
import WatchConnectivity


class WatchViewModel:NSObject,ObservableObject {
    private let session: WCSession
    
    init(session: WCSession = .default) {
        self.session = session
        super.init()
        self.session.delegate = self
        self.session.activate()
    }
    
    func sendMsg(sendData:[String:String],isCompletion:(_:(Bool) -> Void)) {
//        let msg = ["name":"张三"]
        guard session.activationState == .activated else{
            print("发送失败，状态不为active")
            isCompletion(false)
            return
        }
//        session.sendMessage(sendData) { res in
//            print(res)
//            isCompletion(true)
//        } errorHandler: { error in
//            isCompletion(false)
//        }
//
//        session.sendMessage(sendData) { res in
//            print(res)
//            isCompletion(true)
//        }
        session.sendMessage(sendData) { res in
            isCompletion(true)
            print(res)
        } errorHandler: { error in
            isCompletion(false)
            print(error)
        }

//        session.sendMessage(sendData, replyHandler: nil) { res in
//            if res != nil {
//                isCompletion(false)
//            }
//            isCompletion(true)
//            print(res)
//        }
    }
}




extension WatchViewModel:WCSessionDelegate {
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: (any Error)?) {
        
    }
    
    
    
}
